<?php

namespace app\agent\controller;

use app\admin\library\Auth;
use app\admin\model\Admin;
use app\common\controller\Agent;
use app\admin\model\halt\Cate;
use app\admin\model\orders\Shopping as ShoppintCar;
use app\admin\model\Preproduct;
use EasyWeChat\Factory;
use app\admin\model\Stock;
use think\Db;
class index extends Agent
{

    protected $model = null;
    protected $searchFields = 'cname';
     protected $noNeedRight = '*';
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new Preproduct();
        //设置分类
        $haltCate = null;
        $typeall = 0;
       
        $preCate = Cate::field('id,pid,name')->where(array("type"=>1,"pid"=>0))->order("id asc")->select();
        $firstCate=$preCate[0]['id'];
        $cate = Cate::field('id,pid,name')->where(array("pid"=>$firstCate))->order("id asc")->select();
        foreach ($cate as $k=>$v){
            $cate[$k]['cateArr']=Db::name("halt_cate")->field('id,pid,name')->where(array("pid"=>$v['id']))->order("id asc")->select();
        }

        $this->view->assign("preCate", $preCate);
        $this->view->assign("cate", $cate);

        $this->view->assign('typeall',$typeall);
        $this->view->assign('shoppingCar',ShoppintCar::shoppingCart($this->auth->id,1));
    }

    public function index()
    {
        $discount =  '';
        $product = null;
        if($discount)
        {
            $arr = [];
            foreach ($discount as $key=>$value)
            {
                $arr[] = "%$key,%";
            }
            $product['class_text'] =array('like',$arr,'OR');
        }
        $json = $this->request->request('filter');
        $json = json_decode($json,true);
        if($json['class_text'])
        {
             unset($product['class_text']);
        }
        $anywhere['cid']=1;
        $anywhere['num']=array('gt',0);
        $stock=array_column(Db::name('stock')->where($anywhere)->field('shopid as id,cid,num')->select(),'id');
        if (!$stock){
            $this->view->assign('list',[]);
            $this->view->assign('total',0);
        }else{
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $limit = 1000;
            $wheres['id']=array('in',implode(',',$stock));
            $list = $this->model
                ->where($where)
                 ->where($wheres)
                ->where($product)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            foreach($list as $k=>$v){
                if ($v['type']==2){
                    $info[$k]['type']="成品";
                }else{
                    $info[$k]['type']="半成品";
                }

                $info[$k]['storenum']=$this->getStore($v['id'],1);
            }
            $this->view->assign('list',$list);
         
        }
        //获取商品列表  type=2成品  classif=分类
        return $this->view->fetch();
    }
    protected function getStore($id,$cid=1){
        $db = new Stock();
        $info = $db->where(array('cid'=>$cid,'shopid'=>$id))->find();
        if (!$info)return 0;
        return $info['num'];
    }
    
    function getCate(){
        if (isset($_POST['cate1'])){
            $cate1 = intval($_POST['cate1']);
            $cate = Db::name("halt_cate")->field('id,pid,name')->where(array("pid"=>$cate1))->order("id asc")->select();

            if (!$cate)return json(array("code"=>0));
            $str='';
            foreach ($cate as $v){
                $str.='  <div class="cate_content_title">
                            <span class="h"></span><span>'.$v['name'].'</span><span class="h"></span>
                        </div>';
                $list = Db::name("halt_cate")->field('id,pid,name')->where(array("pid"=>$v['id']))->order("id asc")->select();
                $str.=' <table cellspacing="0" cellpadding="0">
                            ';
                $i = 1;
                foreach ($list as $d){
                   if($i == 1 || $i-1%3 == 0) $str .'<tr>';
                    $str.=' <td onclick="tdClick(this)" dataid="'.$d['id'].'">'.$d['name'].'</td>';
                    if($i%3 == 0 || $i == count($list)) $str.='   </tr>';
                     $i++;
                }
               
                $str.='</table>';
            }
            return  json(array("code"=>1,'html'=>$str));
        }
    }
    public function getCate1(){
        if (isset($_GET['type'])){
            $type = intval($_GET['type']);
            $where['type']=$type;
            $where['pid']=0;
            if ($type==2){
                $where['id']=array('neq',8);
            }
            $list = Db::name("halt_cate")->where($where)->field('id,pid,name')->select();
            $aid = $list[0]['id'];
            $cate = Db::name("halt_cate")->field('id,pid,name')->where(array("pid"=>$aid))->order("id asc")->select();
            $str='';
            foreach ($cate as $v){
                $str.='  <div class="cate_content_title">
                            <span class="h"></span><span>'.$v['name'].'</span><span class="h"></span>
                        </div>';
                $lists = Db::name("halt_cate")->field('id,pid,name')->where(array("pid"=>$v['id']))->order("id asc")->select();
                $str.=' <table cellspacing="0" cellpadding="0">';
                    $i = 1;         
                foreach ($lists as $d){
                    if($i == 1 || $i-1%3 == 0) $str .'<tr>';
                    $str.=' <td onclick="tdClick(this)" dataid="'.$d['id'].'">'.$d['name'].'</td>';
                    if($i%3 == 0 || $i == count($list)) $str.='   </tr>';
                     $i++;
                }
                $str.=' </table>';
            }
            return json(array('list'=>$list,'html'=>$str));
        }
    }

  
}